package cn.ztydata.teachingsystem.heaven.web.controller;

import cn.ztydata.teachingsystem.common.Page;
import cn.ztydata.teachingsystem.heaven.cons.MsgTemplate;
import cn.ztydata.teachingsystem.heaven.entity.Clazz;
import cn.ztydata.teachingsystem.heaven.service.ClassService;
import cn.ztydata.teachingsystem.heaven.web.dto.ClassDto;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResult;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.text.MessageFormat;

/**
 * 班级控制器
 * Created by fanruiping on 2015/5/5.
 */
@Controller
@RequestMapping("/class")
public class ClassController extends BaseController {
    @Autowired
    private ClassService classService;

    /**
     * 班级列表
     * @param departNum 系部代码
     * @param specNum 专业代码
     * @param tag 标记（0：当前，1：历史）
     * @param page 页码
     * @param pageSize 每页显示条数
     * @return JsonResult
     *
     * @author fanruiping
     * @since 2015-05-05
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult list(@RequestParam(value = "departCode", defaultValue = "") String departNum, @RequestParam(value = "majorCode", defaultValue = "") String specNum,
                           @RequestParam("tag") int tag,@RequestParam("page") int page, @RequestParam("pageSize") int pageSize){
        Assert.isTrue(tag == 0 || tag == 1, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "标记"));
        Assert.isTrue(page > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "当前页码"));
        Assert.isTrue(pageSize > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "每页条数"));

        Page<Clazz> clazzPage = classService.getList(departNum,specNum,tag,page,pageSize);

        Page<ClassDto> classDtoPage = new Page<>(clazzPage.getTotal(),page,pageSize,mapper.mapList(clazzPage.getItems(),ClassDto.class));

        return new JsonResultPage<>(classDtoPage);
    }

    /**
     * 删除指定班级数据
     * @param id 班级编号
     * @return JsonResult
     *
     * @author fanruiping
     * @since 2015-05-05
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult delete(@PathVariable Long id) {
        Assert.isTrue(id > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "班级代码"));

        classService.deleteById(id);

        return new JsonResult(true, "删除班级数据成功");
    }
}
